<template>
  <div class="component-teamCalc">
    <div class="component-mainHeader">
      <div class="top">
        <div class="topleft">
          <p>
            <span class="iconfont" style="color:#1abc9a;margin-right:3px;">&#xe607;</span>施工队信息
          </p>
        </div>
        <div class="topright">
          <el-button type="info" @click="$router.go(0)">
            <span style="font-weight:700;margin-right:5px;" class="iconfont">&#xe662;</span>刷新
          </el-button>
        </div>
      </div>
    </div>
    <div class="main">
      <div class="component-main-left">
        <div class="box1">
          <span class="iconfont">&#xe67e;</span>
          <span>{{editTitle}}</span>
          <div class="arrow"></div>
        </div>
      </div>
      <div class="component-main-right">
        <div class="box senInfo">
          <el-form
            :model="ruleForm"
            :rules="rules"
            ref="ruleForm"
            label-width="100px"
            class="demo-ruleForm exAddWarehouse"
          >
            <el-form-item label="施工队编号:" prop="orgNumber">
              <el-input style="width: 310px!important;" v-model="ruleForm.orgNumber" placeholder="请输入项目部拼音首字母缩写-管/光/设/熔-01"></el-input> 例如：DG-管/光/设-01
            </el-form-item>
            <el-form-item v-if="ms_roleName == 'WorkTeamManager'" label="施工队专业:" prop="checkList">
              <!--1-管道，2-线路，3-设备，4-熔纤，5-跳纤-->
              <el-checkbox-group v-model="ruleForm.checkList">
                <el-checkbox label="1">管道</el-checkbox>
                <el-checkbox label="2">线路</el-checkbox>
                <el-checkbox label="3">设备</el-checkbox>
                <el-checkbox label="4">熔纤</el-checkbox>
                <el-checkbox label="5">补盲扩</el-checkbox>
              </el-checkbox-group>
            </el-form-item>
            <el-form-item label="施工队队长:" prop="userTruename">
              <el-input style="width: 200px!important;" v-model="ruleForm.userTruename" placeholder="请输入施工队长真实姓名"></el-input> 施工队
            </el-form-item>
            <el-form-item label="账号:" prop="userName">
              <el-input v-model="ruleForm.userName" :disabled="$route.params.flag==1?true:false"></el-input>
            </el-form-item>
            <el-form-item v-if="$route.params.flag == 0" label="密码:" prop="userPwd">
              <el-input v-model="ruleForm.userPwd"></el-input>
            </el-form-item>
            <el-form-item v-if="$route.params.flag == 1" label="新密码:" prop="newPwd">
              <el-input v-model="ruleForm.newPwd" placeholder="请输入新密码"></el-input>
            </el-form-item>
            <el-form-item label="联系方式:" prop="userPhone">
              <el-input v-model="ruleForm.userPhone"></el-input>
            </el-form-item>
           <!-- <el-form-item label="所属角色:" prop="role">
              <el-input v-model="ruleForm.role"></el-input>
            </el-form-item>-->
            <el-form-item label="是否启用" prop="isUse">
              <el-switch v-model="ruleForm.isUse"></el-switch>
            </el-form-item>
            <el-form-item>
              <el-button style="margin-left:200px;" type="primary" @click="submitForm('ruleForm')">保存</el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  import area from '@/assets/js/area.js'
  export default {
    name:"teamCalc",
    data(){
      return {
        ms_roleName:localStorage.getItem("roleName"),
        editTitle:'添加施工公司',
        options:area, //省市区
        ruleForm: {
          orgNumber:'',
          checkList:[],
          userTruename:'',
          userName:'',
          userPwd:'',
          userPhone:'',
          role:'',
          isUse:true,
          newPwd:'',
        },
        editRule:{},
        rules: {
          orgNumber: [
            { required: true, message: "请输入施工编号", trigger: "blur" },
            { min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur' }
          ],
          checkList: [
            { type: 'array',required: true, message: "请选择施工队专业", trigger: "change" },
          ],
          userTruename: [
            { required: true, message: "请输入施工队长真实姓名", trigger: "blur" },
            { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }
          ],
          userName: [
            { required: true, message: "请输入账号", trigger: "blur" },
            { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }
          ],
          userPwd: [
            { required: true, message: "请输入密码", trigger: "blur" },
            { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }
          ],
          userPhone: [
            { required: true, message: "请输入联系方式", trigger: "blur" },
            { min: 11, max: 11, message: '请输入11位字符', trigger: 'blur' }
          ],
          role: [
            { required: true, message: "请输入所属角色", trigger: "blur" },
          ],
          newPwd:[
            { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }
          ]
        },
        workarea: ["", "", ""], //省市区
        manageList:[],
        newArray:{}
      };
    },
    mounted(){
//      this.manageLists();//管理员列表
      if(this.$route.params.flag == 0){
        this.editTitle = "添加施工队"
      }else{
        this.ruleForm = this.deepClone(this.$route.params.info);
        this.editTitle = "编辑施工队"
      }
    },
    methods:{
      // 添加
      dataInit(){
        let that = this;
        let suCb = function(res){
          that.$message({
            message: '添加成功',
            type: 'success'
          });
          that.$router.push({name:'exTeamInfo'})
        }
        let erCb = function(res){
          console.log(res)
        }
        let userStatus = '0'
        if(that.ruleForm.isUse==true){
          userStatus = '1'
        }else{
          userStatus = '0'
        }
        let postData = {
          orgNumber: that.ruleForm.orgNumber,
          workFunction: that.ruleForm.checkList.join(","),
          userTruename: that.ruleForm.userTruename,
          userName: that.ruleForm.userName,
          userPwd: "",
          userStatus: userStatus,
          userPhone: that.ruleForm.userPhone
        }
        let palyParam = {
          url: "/api/web/ProConStructionManage/addProTeam",
          methods:'post',
          data: postData,
          success: suCb,
          error: erCb,
        }
        let pwd = that.deepClone(that.ruleForm);
        palyParam.data.userPwd = that.$md5(pwd.userPwd)
        that.$https(palyParam)
      },
      //编辑
      editInit(){
        let that = this;
        let suCb = function(res){
          that.$message({
            message: '编辑成功',
            type: 'success'
          });
          that.$router.push({name:'exTeamInfo'})
        }
        let erCb = function(res){
          console.log(res)
        }
        let userStatus = '0'
        if(that.ruleForm.isUse==true){
          userStatus = '1'
        }else{
          userStatus = '0'
        }
        console.log(that.ruleForm)
        let postData = {
          userId:that.ruleForm.userId,
          orgId:that.ruleForm.orgId,
          orgNumber: that.ruleForm.orgNumber,
          workFunction: that.ruleForm.checkList.join(","),
          userTruename: that.ruleForm.userTruename,
          userName: that.ruleForm.userName,
          userPwd: "",
          userStatus: userStatus,
          userPhone: that.ruleForm.userPhone
        }
        let palyParam = {
          url: "/api/web/ProConStructionManage/updateProTeam",
          methods:'post',
          data: postData,
          success: suCb,
          error: erCb,
        }
        if(that.ruleForm.newPwd == "" || that.ruleForm.newPwd==undefined){
          let pwd = that.deepClone(that.ruleForm);
          palyParam.data.userPwd = pwd.userPwd;
        }else{
          palyParam.data.userPwd = that.$md5(that.ruleForm.newPwd);
        }
        that.$https(palyParam)
      },
      //表单提交
      submitForm(formName){
        console.log(this.ruleForm);
        this.$refs[formName].validate(valid => {
          if (valid) {
            if(this.$route.params.flag == 1){
              this.editInit();
            }else{
              this.dataInit();
            }
          } else {
            console.log("error submit!!");
            return false;
          }
        });
      },
    }
  }
</script>

<style scoped>
  .main{
    overflow: hidden;
    width: 1100px;
    margin: 1px auto;
    border: 1px solid #e4e4e4;
    border-left:none;
    border-top: none;
  }
  .component-main-left {
    width: 180px;
    border: 1px solid #e4e4e4;
    padding-top: 50px;
    float: left;
    height: 673px;
    border-top: none;
    border-bottom: none;
  }
  .box1 {
    position: relative;
    width: 175px;
    height: 50px;
    text-align: left;
    line-height: 50px;
    background-color: #9ea7b4;
    color: #fff;
    padding-left: 5px;
  }
  .box1 .arrow {
    position: absolute;
    width: 0;
    height: 0;
    top: -40px;
    left: 180px;
    border-width: 25px 0 25px 25px;
    border-style: solid;
    border-color: transparent transparent transparent #9ea7b4; /*透明 透明 透明 灰*/
    margin: 40px auto;
  }
  .component-main-right{
    float: left;
  }
  .box{
    margin-left: 120px;
    margin-top: 80px;
    outline: none;
  }
  .el-button--primary{
    background-color: #1ABC9C;
    border-color: #1ABC9C;
  }

</style>
